package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.List;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:12 AM
 */
@Table(name = "section", catalog = "damate")
@Entity
public class Section {

    private Long sectionId;

    @Column(name = "sectionId", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @GeneratedValue(strategy= IDENTITY)
    public Long getSectionId() {
        return sectionId;
    }

    public void setSectionId(Long sectionId) {
        this.sectionId = sectionId;
    }

    private String sectionName;

    @Column(name = "sectionName", nullable = false, insertable = true, updatable = true, length = 200, precision = 0)
    @Basic
    public String getSectionName() {
        return sectionName;
    }

    public void setSectionName(String sectionName) {
        this.sectionName = sectionName;
    }

    private String sectionCode;

    @Column(name = "sectionCode", nullable = true, insertable = true, updatable = true, length = 45, precision = 0)
    @Basic
    public String getSectionCode() {
        return sectionCode;
    }

    public void setSectionCode(String sectionCode) {
        this.sectionCode = sectionCode;
    }

    private User sectionLeader;

    @ManyToOne(fetch = LAZY, cascade = REFRESH)
    @JoinColumn(name = "sectionLeader")
    public User getSectionLeader() {
        return sectionLeader;
    }

    public void setSectionLeader(User sectionLeader) {
        this.sectionLeader = sectionLeader;
    }

    private List<Team> teams;

    @ManyToMany(fetch = LAZY, mappedBy = "sections", cascade = REFRESH)
    @OrderBy("teamName asc")
    public List<Team> getTeams() {
        return teams;
    }

    public void setTeams(List<Team> teams) {
        this.teams = teams;
    }

    private List<User> sectionManagers;

    @ManyToMany(fetch = LAZY, cascade = REFRESH)
    @OrderBy("firstname, lastname asc")
    @JoinTable(name = "section_has_user",
               joinColumns = { @JoinColumn(name = "section_sectionId")},
               inverseJoinColumns={@JoinColumn(name="user_username")})
    public List<User> getSectionManagers() {
        return sectionManagers;
    }

    public void setSectionManagers(List<User> sectionManagers) {
        this.sectionManagers = sectionManagers;
    }

    private List<Task> tasks;

    @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "section")
    @OrderBy("name asc")
    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Section that = (Section) o;

        if (sectionId != that.sectionId) return false;
        if (sectionCode != null ? !sectionCode.equals(that.sectionCode) : that.sectionCode != null) return false;
        if (sectionName != null ? !sectionName.equals(that.sectionName) : that.sectionName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sectionId.intValue();
        result = 31 * result + (sectionName != null ? sectionName.hashCode() : 0);
        result = 31 * result + (sectionCode != null ? sectionCode.hashCode() : 0);
        return result;
    }
}
